package com.unknowpupil.web.service.impl;

import com.google.common.collect.Maps;
import com.unknowpupil.web.base.Result;
import com.unknowpupil.web.base.impl.BaseCrudServiceImpl;
import com.unknowpupil.web.component.wallet.entity.CoinWallet;
import com.unknowpupil.web.component.wallet.service.CoinToolsService;
import com.unknowpupil.web.domain.Account;
import com.unknowpupil.web.domain.AccountInfo;
import com.unknowpupil.web.domain.Wallet;
import com.unknowpupil.web.exception.MxException;
import com.unknowpupil.web.service.IWalletService;
import com.unknowpupil.web.tool.ConstantsTool;
import com.unknowpupil.web.tool.CreateInvitationWordTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.unknowpupil.web.mapper.AccountMapper;
import com.unknowpupil.web.service.IAccountService;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname: ${NAME}
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/11/6 14:20
 * @Description:     //耦合度太高，按道理来说应该，耦合低是每个模块都是独立运行的模块，很明显这个模块本身就是一个单独的业务模块，
 *                   但是去依赖了很多其他业务的，应该提起一个单独的类来做这些东西这个模块就只包含这个模块的东西
 * @Version: 1.0
 */
@Service
public class AccountServiceImpl extends BaseCrudServiceImpl<Account,AccountMapper> implements IAccountService {
    @Autowired
    private CoinToolsService coinToolsService;
    @Resource
    private AccountMapper accountMapper;
    @Autowired
    private CreateInvitationWordTool createInvitationWordTool;
    @Autowired
    private AccountInfoServiceImpl accountInfoService;
    @Autowired
    private IWalletService walletService;

    @Override
    @Transactional(rollbackFor = MxException.class)
    public Result createWallet(String userName, String password, String invitationCode) {
        try {
            //生成用户
            Account  account = createAccount(userName,password);
            //生成用户信息
            if(account.getId()==null){
              throw new MxException("生成钱包出错",1);
            }
            CoinWallet ethWallet = coinToolsService.createWallet(password);
            AccountInfo accountInfo = accountInfoService.createAccountInfo(password, account,ethWallet);
            boolean wallet = walletService.createWallet(accountInfo);
            //todo: 如果有等级需要写入等级关系
            HashMap<String, String> map = Maps.newHashMap();
            map.put("MnemonicWord",accountInfo.getMnemonicCode());
            map.put("source",ethWallet.getFileName());
            return Result.ok(map);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Result findSecretKeyBySource(String password, String source) throws MxException {
        CoinWallet coinWallet = coinToolsService.readWalletBySource(password, source);
        return Result.ok(coinWallet);
    }

    @Override
    public Account createAccount(String userName, String password) throws MxException {
        Account account = new Account();
        account.setUserName(userName);
        account.setPassword(password);
        ConstantsTool.entryptPassword(account);
        account.setInvitationCode(createInvitationWordTool.createInvitationCode(null));
        account.setStatus(0);
        account.setDelFlag(0);
        return save(account);
    }

    @Override
    public Result getAccount(String id) {
        Account account = accountMapper.selectByPrimaryKey(id);
        account.setPassword(null);
        account.setSalt(null);
        return Result.ok(account);
    }



}
